package com.coolrui.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.coolrui.common.system.ApiList;
import com.coolrui.common.system.ApiResult;
import com.coolrui.common.system.Response;
import com.coolrui.common.utils.MockDataUtils;
import com.coolrui.converter.UserConverter;
import com.coolrui.domain.user.entity.UserAccount;
import com.coolrui.model.UserCreateRequest;
import com.coolrui.domain.user.service.UserHandleService;
import com.coolrui.model.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 * @author coolrui
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserHandleService userHandleService;
    @Autowired
    private UserConverter userConverter;
    @Autowired
    private MockDataUtils mockDataUtils;

    /**
     * 批量创建测试用户
     * @param userNum 创建用户数量
     * @return 用户列表
     */
    @GetMapping("createMockUser")
    public ApiResult<List<UserVO>> createMockUser(@RequestParam(defaultValue = "1") Long userNum) {
        List<UserVO> userList = new ArrayList<>();
        for (int i = 0; i < userNum; i++) {
            UserAccount user = userHandleService.getMockUser();
            userHandleService.createUser(user);
            //转换展示实体
            UserVO userVO = userConverter.toUserVO(user);
            userList.add(userVO);
        }
        log.info("创建测试用户{}个，userList：{}", userNum,JSONUtil.toJsonStr(userList));
        return Response.makeOKRsp(userList);
    }

    @GetMapping("getUser")
    public ApiResult<UserVO> getUser(@RequestParam Long userId) {
        UserAccount user = userHandleService.getUser(userId);
        //转换展示实体
        UserVO userVO = userConverter.toUserVO(user);
        return Response.makeOKRsp(userVO);
    }

    @GetMapping("getUserList")
    public ApiResult<ApiList<UserVO>> getUserList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "0") Long cityId
    ) {
        ApiList<UserAccount> userList = userHandleService.getUserList(cityId, page, pageSize);
        //转换展示实体
        List<UserVO> userVOList = userConverter.toUserVO(userList.getList());
        return Response.makeOKRsp(ApiList.makeResult(userList.getHasMore(), userVOList));
    }

    @PostMapping("createUser")
    public ApiResult<UserVO> createUser(@Valid @RequestBody UserCreateRequest request) {
        //创建数据
        UserAccount user = new UserAccount();
        user.setUserId(IdUtil.getSnowflakeNextId());
        user.setGender(request.getSex());
        user.setAge(request.getAge());
        user.setName(request.getName());
        user.setCityId(mockDataUtils.getCityId());
        user.setMobile(request.getMobile());
        user.setAddress(request.getAddress());
        int result = userHandleService.createUser(user);
        //转换展示实体
        UserVO userVO = userConverter.toUserVO(user);
        return Response.makeOKRsp(userVO);
    }
}